package com.open.party.cloud.viewModel;

import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.*
import com.open.party.cloud.model.data.Api
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class ZiYuanZheViewModel {

//    /**
//     * 志愿者主页数据
//     */
//    fun getZiYuanZheMainData(pageReq: PageReq<NewsVo>) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .getZiYuanZheMainData(pageReq)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<AppPageData<NewsBean>>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    EventBus.getDefault().post(ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message)))
//                }
//
//                override fun onNext(result: ResultInfo<AppPageData<NewsBean>>) {
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }

    /**
     * 团队列表
     */
    fun getZyzTuanDuiListData(pageReq: PageReq<ZyzTuanDuiEntity>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getZyzTuanDuiListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzTuanDuiEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<ZyzTuanDuiEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getZyzTuanDuiListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<AppPageData<ZyzTuanDuiEntity>>) {
                    result.eventType = "getZyzTuanDuiListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 团队》团队成员
     */
    fun getZyzTuanDuiMemberListData(pageReq: PageReq<ZyzTuanDuiVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getZyzTuanDuiMemberListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<VolunteerMemberEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    EventBus.getDefault().post(ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message)))
                }

                override fun onNext(result: ResultInfo<AppPageData<VolunteerMemberEntity>>) {
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 团队详情
     */
    fun getZyzTuanDuiDetail(teamId: String) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getZyzTuanDuiDetail(teamId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ZyzTuanDuiEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    EventBus.getDefault().post(ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message)))
                }

                override fun onNext(result: ResultInfo<ZyzTuanDuiEntity>) {
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 活动列表
     */
    fun getHuoDongListData(pageReq: PageReq<VolunteerVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getHuoDongListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzHuoDongEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<ZyzHuoDongEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getHuoDongListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<AppPageData<ZyzHuoDongEntity>>) {
                    result.eventType = "getHuoDongListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 活动列表> 我的
     */
    fun getHuoDongForMeListData(pageReq: PageReq<VolunteerVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getHuoDongForMeListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzHuoDongEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<AppPageData<ZyzHuoDongEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getHuoDongForMeListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<AppPageData<ZyzHuoDongEntity>>) {
                    result.eventType = "getHuoDongForMeListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 活动详情
     */
    fun getHuoDongDetail(id: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getHuoDongDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ZyzHuoDongEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = id
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<ZyzHuoDongEntity>) {
                    result.eventType = id
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 活动报名
     */
    fun huoDongSignUp(vo: ZyzHuoDongVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .huoDongSignUp(vo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ZyzHuoDongBMEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = vo.requestId
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<ZyzHuoDongBMEntity>) {
                    result.eventType = vo.requestId
                    EventBus.getDefault().post(result)
                }
            })
    }
//
//    /**
//     * 帮助列表
//     */
//    fun getBangWoListData(pageReq: PageReq<ZyzBangWoEntity>) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .getBangWoListData(pageReq)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzBangWoEntity>>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    val res = ResultInfo<ZyzBangWoEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
//                    res.eventType = "getBangWoListData"
//                    EventBus.getDefault().post(res)
//                }
//
//                override fun onNext(result: ResultInfo<AppPageData<ZyzBangWoEntity>>) {
//                    result.eventType = "getBangWoListData"
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
//    fun addBangWo(bean: ZyzBangWoEntity) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .addBangWo(bean)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<ZyzBangWoEntity>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    EventBus.getDefault().post(ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message)))
//                }
//
//                override fun onNext(result: ResultInfo<ZyzBangWoEntity>) {
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
    /**
     * 获取登录用户的志愿者信息
     */
    fun getZyzLoginUserInfo() {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .zyzLoginUserInfo
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<VolunteerMemberEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    EventBus.getDefault().post(VolunteerMemberEntity())
                }

                override fun onNext(result: ResultInfo<VolunteerMemberEntity>) {
                    EventBus.getDefault().post(result.data)
                }

            })
    }

    // 新增志愿者信息
    fun zyzAddUserInfo(volunteer: VolunteerMemberEntity) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .zyzAddUserInfo(volunteer)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<Boolean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<Boolean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "zyzAddUserInfo"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<Boolean>) {
                    result.eventType = "zyzAddUserInfo"
                    EventBus.getDefault().post(result)
                }
            })
    }
//
//    // 编辑志愿者信息
//    fun zyzEditUserInfo(volunteer: VolunteerMemberEntity) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .zyzEditUserInfo(volunteer)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<Boolean>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    EventBus.getDefault().post(ResultInfo<Boolean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message)))
//                }
//
//                override fun onNext(result: ResultInfo<Boolean>) {
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
    /**
     * 活动签到
     */
    fun doActivitySignIn(kqVo: ZyzKaoQingBean) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .doActivitySignIn(kqVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<Boolean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<Boolean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "doActivitySignIn"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<Boolean>) {
                    result.eventType = "doActivitySignIn"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getZyzQianDaoListData(pageReq: PageReq<ZyzKaoQingBean>) { //, view: ZyzSignInMainFragment
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getZyzQianDaoListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzKaoQingBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<AppPageData<ZyzKaoQingBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getZyzQianDaoListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<AppPageData<ZyzKaoQingBean>>) {
                    result.eventType = "getZyzQianDaoListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 获取积分榜数据
     */
    fun getZyzJiFenOrderListData(pageReq: PageReq<ZyzJiFenBean>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getZyzJiFenOrderListData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<ZyzJiFenBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<AppPageData<ZyzJiFenBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getZyzJiFenOrderListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<AppPageData<ZyzJiFenBean>>) {
                    result.eventType = "getZyzJiFenOrderListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 获取积分榜单位数据
     */
    fun getZyzJiFenStreetListData() {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .zyzJiFenStreetListData
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ArrayList<ZyzJiFenBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<ArrayList<ZyzJiFenBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getZyzJiFenStreetListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<ArrayList<ZyzJiFenBean>>) {
                    result.eventType = "getZyzJiFenStreetListData"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getTimeListData(activityId: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getTimeListData(activityId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<ArrayList<TimeBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val res = ResultInfo<ArrayList<TimeBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    res.eventType = "getTimeListData"
                    EventBus.getDefault().post(res)
                }

                override fun onNext(result: ResultInfo<ArrayList<TimeBean>>) {
                    result.eventType = "getTimeListData"
                    EventBus.getDefault().post(result)
                }
            })
    }
//
//    /**
//     * 积分红豆》列表
//     */
//    fun getHongDouListData(pageReq: PageReq<JiFenBean>, eventType: String) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .getHongDouListData(pageReq)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<AppPageData<JiFenBean>>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    val res = ResultInfo<JiFenBean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
//                    res.eventType = eventType
//                    EventBus.getDefault().post(res)
//                }
//
//                override fun onNext(result: ResultInfo<AppPageData<JiFenBean>>) {
//                    result.eventType = eventType
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
//    // 积分红豆》查询：模块{1.积分，2红豆}
//    fun queryJiFenInfo(vo: JiFenVo) {
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .queryJiFenInfo(vo)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<Int>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    val res = ResultInfo<Int>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
//                    res.eventType = "queryJiFenInfo"
//                    EventBus.getDefault().post(res)
//                }
//
//                override fun onNext(result: ResultInfo<Int>) {
//                    result.eventType = "queryJiFenInfo"
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
//    // 积分红豆》兑换
//    fun jiFenExchange(vo: JiFenVo) {
//        if (!XUtils.net().isConnected) {
//            val resultInfo = ResultInfo<String>(ResultInfo.ERROR, "网络不可用")
//            resultInfo.eventType = "jiFenExchange"
//            EventBus.getDefault().post(resultInfo)
//            return
//        }
//
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .jiFenExchange(vo)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<Boolean>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    val res = ResultInfo<Boolean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
//                    res.eventType = "jiFenExchange"
//                    EventBus.getDefault().post(res)
//                }
//
//                override fun onNext(result: ResultInfo<Boolean>) {
//                    result.eventType = "jiFenExchange"
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
//
//    fun loadOrgList(parentId: String) {
//        if (!XUtils.net().isConnected) {
//            val resultInfo = ResultInfo<ArrayList<OrgTreeBean>>(ResultInfo.ERROR, "网络不可用")
//            resultInfo.eventType = "loadOrgList"
//            EventBus.getDefault().post(resultInfo)
//            return
//        }
//
//        val pageReq = PageReq<OrgTreeBean>()
//        pageReq.pageNo = 1
//        pageReq.pageSize = 120
//
//        val vo = OrgTreeBean()
//        vo.parentId = parentId
//        pageReq.data = vo
//
//        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
//            .create(Api::class.java)
//            .loadOrgList(pageReq)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Subscriber<ResultInfo<ArrayList<OrgTreeBean>>>() {
//
//                override fun onCompleted() {}
//
//                override fun onError(e: Throwable) {
//                    val res = ResultInfo<ArrayList<OrgTreeBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
//                    res.eventType = "loadOrgList"
//                    EventBus.getDefault().post(res)
//                }
//
//                override fun onNext(result: ResultInfo<ArrayList<OrgTreeBean>>) {
//                    result.eventType = "loadOrgList"
//                    EventBus.getDefault().post(result)
//                }
//            })
//    }
}
